package cn.lanqiao.demo.controller;

import cn.lanqiao.demo.entity.Leave;
import cn.lanqiao.demo.service.LeaveService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
* 学生请假Controller类
* */
@RestController
@RequestMapping("/leave")
public class LeaveController {
    @Resource
    LeaveService leaveService;

    /*学生添加请假条；成功返回ok字符串，否则返回error字符串*/
    @GetMapping("/add")
    public String addLeave(String Sname, String reason, String time,String place, String status, String Sno) throws Exception {
       int num = leaveService.addLeave(Sname,reason,time,place,status,Sno);
       if (num > 0){
           return "ok";
       }else {
           return "error";
       }
    }

    /*学生请假条信息展示*/
    @GetMapping("/show")
    public List<Leave> showLeaveBySno(String Sno) throws Exception {
        List<Leave> list = leaveService.showLeaveBySno(Sno);
        return list;
    }



    /*学生修改请假条信息*/
    @GetMapping("/update")
    public String updateLeaveBySno(String reason, String time, String place, String Sno) throws Exception {
        int num = leaveService.updateLeaveBySno(reason,time,place,Sno);
        if (num > 0){
            return "ok";
        }else {
            return "error";
        }
    }

    /*学生通过假条编号Lno删除假条*/
    @GetMapping("/delete")
    public String delete(int Lno) throws Exception{
        int num = leaveService.delete(Lno);
        if (num > 0){
            return "ok";
        }else{
            return "error";
        }
    }

    /*显示所有假条*/
    @GetMapping("/showAll")
    public List<Leave> showAll() throws Exception{
        List<Leave> list = leaveService.showAll();
        return list;
    }

    /*查询所有 未批阅 的学生假条，status=0的*/
    @GetMapping("/notCheck")
    public List<Leave> notCheck() throws Exception{
        List<Leave> list = leaveService.notCheck();
        return list;
    }

    /*老师不同意批假,通过学生Lno假条编号修改假条状态status为 1（0带批阅，1不同意，2同意）*/
    @GetMapping("/unallowLeave")
    public List<Leave> unallowLeave(int Lno) throws Exception{
        int num = leaveService.unallowLeave(Lno);
        /*同意一个学生假条之后，通过vue双向绑定，刷新集合中的假条*/
        List<Leave> list = leaveService.notCheck();
        return list;
    }

    /*老师同意学生请假，status从1变成2*/
    @GetMapping("/allowLeave")
    public List<Leave> allowLeave(int Lno) throws Exception{
        int num = leaveService.allowLeave(Lno);
        /*同意一个学生假条之后，通过vue双向绑定，刷新集合中的假条*/
        List<Leave> list = leaveService.notCheck();
        return list;
    }

}
